package core.sim;

import stats.*;
import utility.*;
import core.*;
import core.genetic.*;

public class RoundsEvaluation implements Evaluation{

	private final int rounds;
	
	public RoundsEvaluation(int rounds) {
		this.rounds = rounds;
	}

	@Override
	public double[] evaluate(Population pop) {
		int n = pop.size();
		double[] res = new double[n];
			
		Individual[] team = new Individual[ Mode.mode.teamNb];
		for(int tt = 0;tt<rounds;tt++) {
			int[] perm = Utils.randomPermumation(n);
			for(int k = 0;k<n;k+= Mode.mode.teamNb) {
				for(int i = 0;i< Mode.mode.teamNb;i++)
					team[i] = pop.get(perm[k+i]);
				double[] points =  Mode.mode.simulation.playSimulation(team);
				for(int i = 0;i< Mode.mode.teamNb;i++)
					res[perm[k+i]] += points[i];
			}
		}		
		
//		for(int i = 0;i<n;i++)
//			Statistics.addCost(res[i]);
		
		Statistics.addTotalCosts(res, 1.0/(rounds));
		
		return res;
	}

	@Override
	public void executeForMovie(Population pop) {
		Individual[] individuals = new Individual[Mode.mode.teamNb];			
		int n = individuals.length;					

		double[] values = evaluate(pop);
		
		double[] pom = Utils.calcCumulativeProb(values, values.length);
		
		for(int i = 0;i<n;i++)
			individuals[i] = new Individual(pop.get(Utils.randomW(pom)));

		Mode.mode.simulation.executeForMovie(individuals);
		
	}
	
	
}
